package org.hxzon.cyclone.plinks;

import java.util.List;

import org.hxzon.cyclone.Vector3;
import org.hxzon.cyclone.pcontacts.ParticleContact;

//连杆，长度固定
public class ParticleRod extends ParticleLink {
    public double length;

    public int addContact(List<ParticleContact> contacts) {
        double currentLen = currentLength();

        if (currentLen == length) {
            return 0;
        }

        ParticleContact contact = new ParticleContact();
        contacts.add(contact);

        contact.particle[0] = particle[0];
        contact.particle[1] = particle[1];

        Vector3 normal = particle[1].getPosition().minus(particle[0].getPosition());
        normal.normalise();

        // 被拉伸，或者被挤压
        if (currentLen > length) {
            contact.contactNormal = normal;
            contact.penetration = currentLen - length;
        } else {
            contact.contactNormal = normal.multi(-1);
            contact.penetration = length - currentLen;
        }

        // 总是没有反弹力，恢复系数为0
        contact.restitution = 0;

        return 1;
    }

}
